﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AntikCompta.Data;

namespace AntikCompta
{
    public partial class StockWnd : Form
    {
        private Stock mStock;
        private String mSelectedRef;
        private int mAnnee;
        internal StockWnd(String aTitre, ref Stock aStock, int aAnnee)
        {
            mAnnee = aAnnee;
            mStock = aStock;
            if( mStock!=null)
                mStock.DataChanged += new DataChangedHandler(stok_DataChanged);

            CouleurLotMng.GetObject().ColorChanged += new DataChangedHandler(StockWnd_ColorChanged);

            InitializeComponent();
            Text = aTitre;
            RefreshView();
        }

        void StockWnd_ColorChanged()
        {
            RefreshView();
        }

        void stok_DataChanged()
        {
            RefreshView();
        }

        private void RefreshView()
        {
            if (mStock == null)
                return;

            float valeur = 0;
            mStockView.Rows.Clear();
            List<Produit> liste = mStock.GetDispo(mAnnee);

            if (liste.Count > 0)
            {
                mStockView.Rows.Add(liste.Count);

                int row = 0;
                while (row < liste.Count)
                {
                    mStockView.Rows[row].Cells[0].Value = liste[row].Reference;
                    mStockView.Rows[row].Cells[1].Value = liste[row].DateAchat;
                    mStockView.Rows[row].Cells[2].Value = liste[row].Nom;
                    float prix = liste[row].Vendeur.GetPrixAchat(liste[row].PrixAchat);
                    mStockView.Rows[row].Cells[3].Value = prix;
                    mStockView.Rows[row].Cells[4].Value = liste[row].Vendeur.Nom;
                    mStockView.Rows[row].Cells[5].Value = liste[row].ModePayementAchat.Nom;

                    Color couleur;
                    if (CouleurLotMng.GetObject().GetColor(Produit.GetLotRef(liste[row].Reference), out couleur))
                        mStockView.Rows[row].DefaultCellStyle.BackColor = couleur;

                    valeur += prix;
                    row++;
                }
            }
            l_valeur.Text = Math.Round(valeur, 2) + " €";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (mStockView.SelectedRows != null)
            {
                if (mStockView.SelectedRows.Count > 0)
                    mSelectedRef = mStockView.SelectedRows[0].Cells[0].Value.ToString();
            }

            DialogResult = DialogResult.OK;
            Close();
        }


        internal String GetSelectedReference()
        {
            return mSelectedRef;
        }

        private void StockWnd_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (mStock != null)
                mStock.DataChanged -= new DataChangedHandler(stok_DataChanged);

            CouleurLotMng.GetObject().ColorChanged -= new DataChangedHandler(StockWnd_ColorChanged);
        }

        private void mStockView_SelectionChanged(object sender, EventArgs e)
        {
            txt_status.Text = "";
            if ((mStockView.SelectedRows == null) || (mStockView.SelectedRows.Count <= 0))
                return;

            float valeur = 0;
            foreach (DataGridViewRow row in mStockView.SelectedRows)
            {
                if (row.Cells[3].Value != null)
                    valeur += (float)row.Cells[3].Value;
            }
            txt_status.Text = "somme = " + Math.Round(valeur, 2) + " €";
        }

        private void bt_couleur_Click(object sender, EventArgs e)
        {
            if ((mStockView.SelectedRows == null) || (mStockView.SelectedRows.Count <= 0))
                return;

            String reference = mStockView.SelectedRows[0].Cells[0].Value.ToString();

            CouleurLotWnd wnd = new CouleurLotWnd(Produit.GetLotRef(reference));
            wnd.ShowDialog();
        }
    }

    public class StockWndViewer : StockWnd
    {
        internal StockWndViewer(String aTitre, ref Stock aStock, int aAnnee)
            : base(aTitre, ref aStock, aAnnee)
        {
            mStockView.MultiSelect = true;
        }
    }
}
